package com.example.esquel_carpool.ui.home;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.ImageButton;
import com.example.esquel_carpool.R;
import com.example.esquel_carpool.adapter.HomeAdapter;
import com.example.esquel_carpool.data.HomeListData;
import com.example.esquel_carpool.entity.home.HomeData;
import com.example.esquel_carpool.entity.home.ListBean;
import com.example.esquel_carpool.ui.detail.DetailActivity;
import com.example.esquel_carpool.utils.RecycleViewDivider;
import com.google.gson.Gson;

public class Main_Activity extends AppCompatActivity implements HomeAdapter.OnItemClickListener {

    private static final String TAG = "Main_Activity";

    private Toolbar toolbar2;

    private TabLayout mTabLayout;

    private RecyclerView mRecyclerView;

    private HomeAdapter homeAdapter;

    private String[] titles = new String[]{"全部","女士服装","个护美妆","户外运动","3C数码","居家"};

    private Gson gson = new Gson();

    private ImageButton mImageButton;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        toolbar2 = findViewById(R.id.toolbar2);
        toolbar2.inflateMenu(R.menu.home_menu);

        mTabLayout = findViewById(R.id.mTabLayout);
        for (int i = 0; i < titles.length; i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(titles[i]));
        }

        mRecyclerView = findViewById(R.id.mRecyclerView);
        HomeData homeData = gson.fromJson(HomeListData.JSON, HomeData.class);
//        Log.d(TAG, "onCreate: "+homeData.toString());
        homeAdapter=new HomeAdapter(this,homeData.getData().getList());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        mRecyclerView.setAdapter(homeAdapter);


        homeAdapter.setOnItemClickListener(this);

        mRecyclerView.addItemDecoration(new RecycleViewDivider(
                this, LinearLayoutManager.VERTICAL, 1, getResources().getColor(R.color.divide_gray_color)));


        mImageButton = findViewById(R.id.mImageButton);

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            private int distance;

            private boolean visible = true;

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if(distance < -ViewConfiguration.getTouchSlop() && !visible){
                    //显示fab

                    showFABAnimation(mImageButton);
                    distance = 0;
                    visible = true;
                }else if(distance > ViewConfiguration.getTouchSlop() && visible){
                    //隐藏

                    hideFABAnimation(mImageButton);
                    distance = 0;
                    visible = false;
                }
                if ((dy > 0 && visible) || (dy < 0 && !visible)){
                    //向下滑并且可见  或者  向上滑并且不可见
                    distance += dy;
                }
                int onVisible=visible?View.VISIBLE:View.GONE;
                mImageButton .setVisibility(onVisible);
            }
        });

    }

    /**
     * by moos on 2017.8.21
     * func:显示fab动画
     */
    public void showFABAnimation(View view)
    {
        PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f);
        PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f);
        PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f);
        ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY,pvhZ).setDuration(400).start();
    }

    /**
     * by moos on 2017.8.21
     * func:隐藏fab的动画
     */

    public void hideFABAnimation(View view)
    {
        PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 0f);
        PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 0f);
        PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 0f);
        ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY,pvhZ).setDuration(400).start();

    }

    @Override
    public void onItemClick(int pos, ListBean listBean) {
        DetailActivity.entry(this,listBean);
    }
}
